#ifndef _MIXP_CORE_THREADPOOL_H
#define _MIXP_CORE_THREADPOOL_H

#include <pthread.h>
#include "core/mixp_core.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define MAX_THREAD_TASK_QUEUE_SIZE (65536)
#define MAX_THREADS (128)

    struct mixp_thread_task;

    typedef struct mixp_thread_pool
    {
        pthread_mutex_t lock;
        pthread_cond_t notify;

        pthread_t *threads;
        struct mixp_thread_task *queue;

        int n_threads; //线程数量
        int size;      //任务队列长度

        int head;     //任务队列头
        int tail;     //任务队列尾
        int count;    //待运行的任务数量
        int shutdown; //是否关闭
        int started;  //正在指向任务的线程数
    } mixp_thread_pool_t;

    typedef enum mixp_threadpool_error
    {
        INVALID = -1,
        LOCK_FAILED = -2,
        QUEUE_IS_FULL = -3,
        SHUTDOWN = -4,
        THREAD_FAILTURE = -5
    } mixp_threadpool_error_t;

    typedef enum mixp_threadpool_destroy_flag
    {
        GRACEFULLY = 1
    } mixp_threadpool_destroy_flag_t;

    mixp_thread_pool_t *mixp_threadpool_new(int, int);
    int mixp_threadpool_add_task(mixp_thread_pool_t *, void (*routine)(void *), void *, int);
    int mixp_threadpool_delete(mixp_thread_pool_t *, int);

#ifdef __cplusplus
}
#endif
#endif
